package modellayer;
import java.util.ArrayList;

public class Ordre 
{

    private String dato;
    private double totalPris;
    private int ordreNr;
    private String status;
    private static int ordreNo = 0;
    private String toString = "";
    private ArrayList<Vare> vList;

    //Kassesalg
    /**
     * Ordre Constructor. This is for regular sales at the register.
     *
     * @param dato Date
     */
    public Ordre(String dato)
    {
        ordreNo++;
        this.dato = dato;
        this.ordreNr = ordreNo;
        this.totalPris = 0;
        this.toString = "Kassesalg\n";
        vList = new ArrayList<Vare>();
    }

    //Ordre
    /**
     * Ordre Constructor. Regular order.
     *
     * @param dato Date
     * @param status Status
     * @param totalPris Total price
     */
    public Ordre(String dato, String status, double totalPris) 
    {
        ordreNo++;
        this.dato = dato;
        this.ordreNr = ordreNo;
        this.status = status;
        this.totalPris = 0;
        this.toString = "Ordre\n";
        vList = new ArrayList<Vare>();
    }

    //Tilbud
    /**
     * Ordre Constructor. Offers for regular customers in the system.
     *
     * @param dato Date
     * @param status Status
     */
    public Ordre(String dato, String status) 
    {
        ordreNo++;
        this.dato = dato;
        this.ordreNr = ordreNo;
        this.status = status;
        this.totalPris = 0;
        this.toString = "Tilbud\n";
        vList = new ArrayList<Vare>();
    }
    
    public void ordreNoCounter()
    {
        ordreNo++;
    }
    
    public int getStaticCounter()
    {
        return ordreNo;
    }

    /**
     * Method getVList
     *
     * @return A list with wares. 
     */
    public ArrayList<Vare> getVList()
    {
        return vList;
    }
    
    /**
     * Method addVareToList
     *
     * Adds a ware to an arraylist of wares.
     * 
     * @param v The ware you wish to add
     * @return True if the ware is added. False if not.
     */
    public boolean addVareToList(Vare v)
    {
        boolean succes = false;
        int size = vList.size();
        vList.add(v);        
        if(vList.size() > size)
        {
            succes = true;
        }
        return succes;
    }
        
    /**
     * Method deleteVareFromList
     *
     * Removes a ware with the given ID from the list of wares
     * 
     * @param vareID Ware ID
     * @return False if the ware coulnd't be found. True if the ware was removed.
     */
    public boolean deleteVareFromList(int vareID)
    {
        boolean succes = false;
        Vare v = getVareFromList(vareID);
        if(vList.isEmpty() || v == null)
        {
            return succes;
        }
        vList.remove(v);
        if(!vList.contains(v))
        {
            succes = true;
        }        
        return succes;
    }
    
    /**
     * Method getVareFromList
     *
     * Searches for a ware with the given ID on the orderlist.
     * 
     * @param vareID Ware ID
     * @return A ware of the type Vare
     */
    public Vare getVareFromList(int vareID)
    {
        int i=0;
        boolean found = false;
        while (!found && i < vList.size()) 
        {       
            if (vareID == vList.get(i).getVareID())
            { 
                found = true;
            }
            else 
            {
                i++;
            }
        }
        if(found)
        {
            return vList.get(i);
        }
        else 
        {
            return null;
        }
    }
    
    public Vare getVareFromListStregkode(int stregkode)
    {
        int i=0;
        boolean found = false;
        Vare v = null;
        while (!found && i < vList.size()) 
        {       
            if (stregkode == vList.get(i).getStregkode())
            { 
                v = vList.get(i);
                found = true;
            }
            else 
            {
                i++;
            }
        }
        return v;
    }
        
    /**
     * Method getDato
     *
     * @return Date
     */
    public String getDato() 
    {
        return dato;
    }

    /**
     * Method setDato
     *
     * Sets the date
     * 
     * @param dato Date
     */
    public void setDato(String dato) 
    {
        this.dato = dato;
    }

    /**
     * Method getTotalPris
     *
     * @return Total price
     */
    public double getTotalPris() 
    {
        return totalPris;
    }

    /**
     * Method setTotalPris
     *
     * Set total price
     * 
     * @param totalPris Total price
     */
    public void setTotalPris(double totalPris) 
    {
        this.totalPris = totalPris;
    }

    /**
     * Method getOrdreNr
     *
     * @return The return value
     */
    public int getOrdreNr() 
    {
        return ordreNr;
    }

    /**
     * Method setOrdreNr
     *
     * Set ordernumber 
     * @param ordreNr Ordernumber
     */
    public void setOrdreNr(int ordreNr) 
    {
        this.ordreNr = ordreNr;
    }

    /**
     * Method getStatus
     *
     * @return Status
     */
    public String getStatus() 
    {
        return status;
    }

    /**
     * Method setStatus
     *
     * Set the status 
     * 
     * @param status Status
     */
    public void setStatus(String status) 
    {
        this.status = status;
    }

    /**
     * Method toString
     *
     * Returns a String with information about an order
     * 
     * @return A String
     */
    public String toString()
    {
        toString += "Ordrenummer: " + ordreNr + "\n" + "Dato: " + dato + "\n" + "Totalpris: " + totalPris + "\n" + "Status: " + status + "\n\n";
        return toString;
    }

}
